using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using KS.Common;
using KS.Gantt;

namespace MiGuava
{
    public partial class ucGridView : UserControl
    {

        public object DataSource
        {
            get
            {
                return bindingSource1.DataSource;
            }
            set
            {
                bindingSource1.SuspendBinding();
                bindingSource1.DataSource = value;
                bindingSource1.ResumeBinding();
            }
        }

        protected KS.Gantt.Gantt m_GanttControl = null;
        public KS.Gantt.Gantt GanttControl
        {
            get
            {
                return m_GanttControl;
            }
            set
            {
                if (m_GanttControl != value)
                {
                    m_GanttControl = value;

                    if (m_GanttControl != null)
                    {                        
                        InitData();
                    }
                }
            }
        }        

        public ucGridView()
        {
            InitializeComponent();

            GRD.RowHeadersWidth = 24;
            GRD.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            GRD.ShowEditingIcon = true;            

            GRD.AllowUserToDeleteRows = false;
            GRD.KeyDown += new KeyEventHandler(GRD_KeyDown);            

            GRD.DataError += new DataGridViewDataErrorEventHandler(GRD_DataError);            

            bindingSource1.PositionChanged += new EventHandler(bindingSource1_PositionChanged);
            bindingSource1.DataSourceChanged += new EventHandler(bindingSource1_DataSourceChanged);
            bindingSource1.AddingNew += new AddingNewEventHandler(bindingSource1_AddingNew);
            bindingSource1.ListChanged += new ListChangedEventHandler(bindingSource1_ListChanged);

            mnuFirst.Click += new EventHandler(mnuFirst_Click);
            mnuPrevious.Click += new EventHandler(mnuPrevious_Click);
            mnuNext.Click += new EventHandler(mnuNext_Click);
            mnuLast.Click += new EventHandler(mnuLast_Click);
            mnuNew.Click += new EventHandler(mnuNew_Click);
            mnuDelete.Click += new EventHandler(mnuDelete_Click);
            mnuEdit.Click += new EventHandler(mnuEdit_Click);
            mnuEditGrid.Click += new EventHandler(mnuEditGrid_Click);
            
            GRD.CellValidating += new DataGridViewCellValidatingEventHandler(GRD_CellValidating);            
        }
        
        
        protected virtual void GRD_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (GRD.IsCurrentCellDirty)
            {                
                m_GanttControl.HistorySetUndoPoint();
            }
        }

        // **** Toolbar Events ****

        protected virtual void mnuFirst_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveFirst();
        }

        protected virtual void mnuPrevious_Click(object sender, EventArgs e)
        {
            bindingSource1.MovePrevious();
        }

        protected virtual void mnuNext_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveNext();
        }

        protected virtual void mnuLast_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveLast();
        }

        protected virtual void mnuNew_Click(object sender, EventArgs e)
        {
            
        }        

        protected virtual void mnuDelete_Click(object sender, EventArgs e)
        {
            if (GRD.DataSource == null)
                return;

            if (GRD.SelectedRows.Count == 0)
            {
                if (GRD.CurrentRow != null)
                {
                    if (!GRD.CurrentRow.IsNewRow)
                        GRD.CurrentRow.Selected = true;
                    else
                        return;
                }
                else
                    return;
            }            

            if (!ConfirmDelete())
                return;

            List<object> del = new List<object>();
            foreach (DataGridViewRow row in GRD.SelectedRows)
            {
                if (!row.IsNewRow && row.DataBoundItem != null)
                    del.Add(row.DataBoundItem);
            }

            GRD.CancelEdit();

            if (GRD.SelectedRows.Count > 0)
            {
                if (m_GanttControl != null)
                    m_GanttControl.HistorySetUndoPoint();
            }

            bindingSource1.SuspendBinding();
            m_GanttControl.SuspendItemLayout();                        

            foreach (object o in del)
            {
                ((IBindingList)GRD.DataSource).Remove(o);
            }

            del.Clear();                      

            if (m_GanttControl.ViewMode == Gantt.ViewModes.Resources)
                m_GanttControl.CreateResourceBars();                
            else
                m_GanttControl.AutoMoveItems();

            m_GanttControl.ResumeItemLayout();
            bindingSource1.ResumeBinding();
        }

        void GRD_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Delete:
                    mnuDelete_Click(null, null);
                    e.Handled = true;
                    break;

                case Keys.Enter:                    
                    GRD.BeginEdit(false);
                    e.Handled = true;
                    break;
            }            
        }        

        protected virtual void mnuEdit_Click(object sender, EventArgs e)
        {
            // Override in each instance
        }

        protected virtual void mnuEditGrid_Click(object sender, EventArgs e)
        {
            // Not Implemented
        }

        // ----------------- Toolbar Events -----------------

        void bindingSource1_DataSourceChanged(object sender, EventArgs e)
        {
            EnableControls();
        }

        void bindingSource1_ListChanged(object sender, ListChangedEventArgs e)
        {
            EnableControls();
        }

        void bindingSource1_AddingNew(object sender, AddingNewEventArgs e)
        {            
            EnableControls();
        }

        void bindingSource1_PositionChanged(object sender, EventArgs e)
        {
            EnableControls();            
        }        

        public virtual void EnableControls()
        {
            bool bHasData = bindingSource1.DataSource != null;

            mnuFirst.Enabled = bindingSource1.Position > 0 && bHasData;
            mnuLast.Enabled = bindingSource1.Position < bindingSource1.Count - 1 && bHasData;
            mnuPrevious.Enabled = mnuFirst.Enabled;
            mnuNext.Enabled = mnuLast.Enabled;

            mnuNew.Enabled = bHasData;
            mnuEdit.Enabled = bHasData && GRD.RowCount > 0;
            mnuDelete.Enabled = bHasData;
        }

        protected virtual void SelectGanttItem(GanttItem item)
        {
            if (m_GanttControl == null || GRD.CurrentRow == null)
                return;

            GanttItem t = (GanttItem)GRD.CurrentRow.DataBoundItem;
            if (t != null && t.Key == item.Key)
                return;

            int i = bindingSource1.Find("Key", item.Key);
            if (i >= 0)
                bindingSource1.Position = i;
        }

        // *** Delete ***
        protected virtual bool ConfirmDelete()
        {
            string strMessage = String.Format(Properties.Resources.Question_DeleteRows, GRD.SelectedRows.Count.ToString());
            return Program.QuestionMsgBox(strMessage) == DialogResult.Yes;
        }                

        public virtual void GRD_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            Program.ErrMsgBox(e.Exception.Message);
        }

        public virtual void InitData()
        {            
        }

        public virtual bool EndEdit()
        {
            return GRD.EndEdit();
        }
    }
}
